'''
Created on 2014-2-24

@author: apple
'''
from django.utils.translation import ugettext_lazy as _
from com.ad.sword.web.common import *
from com.ad.sword.dao.models import MonitorTask
import math

@tojson
@check_required_argument
def list(request):
    tasks = string_ifilter(request, MonitorTask.objects, ["name", "types"])
    tasks = sort(request, tasks)
    page_no = int(request.REQUEST.get("page", 1))
    page_size = int(request.REQUEST.get("rows", 40))
    records=tasks.count()
    total=int(math.ceil(float(records)/page_size))
    list = {"items": [],  "page": page_no, "records": str(records),"total":total}
    tasks=tasks.skip((page_no-1) * page_size).limit(page_size)
    for task in tasks:
        u={}
        for k in task:
            if k=='create_time':
                u[k]=time2str(task[k])
            else:
                u[k]=str(task[k])
        list['items'].append(u)

    return list

@tojson
@check_required_argument
def delete(request):
    ids = get_required_argument(request, "id").split(',')
    for id in ids:
        task=MonitorTask.objects.get(id=id)
        task.delete()
    return message(_("OK"))
